package com.shujia.mysql;

import java.sql.*;

/**
 * @Description: MysqlJDBC2 使用PreparedStatement传参添加
 * @Author: 王火火
 * @Date: 2019/9/6 14:13
 */
public class MysqlJDBC22 {
    //创建常量
    static final String DRIVER = "com.mysql.jdbc.Driver";//驱动
    static final String URL = "jdbc:mysql://master:3306/shujia";//路劲
    static final String USERNAME = "root";//用户名
    static final String PASSWORD = "123456";//密码

    public static void main(String[] args) throws Exception {
        //加载驱动

        Class.forName(DRIVER);

        //获取连接
        Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

        //sql语句
        String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,deptno) " +
                "values(?,?,?,?,?,?,?)";
        String xx=null;
        //获取执行器
        PreparedStatement ps  = conn.prepareStatement(sql);
        ps.setInt(1,20);
        ps.setString(2,"null");
        ps.setString(3,"job1");
        ps.setInt(4,50);
        ps.setDate(5,Date.valueOf("2018-03-03"));
        ps.setInt(6,50);
        ps.setInt(7,20);
      //  ps.setInt(8,20);
        //开始执行
        int i  = ps.executeUpdate();
        if (i == 1) {
            System.out.println("添加成功");
        }
        ps.close();
        conn.close();
    }
}
